1 package net.sourceforge.simplegamenet.chess;
2
3 public class ChessKing extends ChessPiece {
4
5 private boolean unmoved = true;
6
7 public ChessKing(int participantOwnerIndex, int x, int y) {
8 super(participantOwnerIndex, x, y);
9 }
10
11 public boolean isMoveAllowed(ChessPiece[] pieceGrid,
12 int destinationX, int destinationY) {
13 if (pieceGrid[destinationX * GRID_HEIGHT + destinationY] != null
14 && pieceGrid[destinationX * GRID_HEIGHT + destinationY]
15 .getParticipantsOwnerIndex() == participantOwnerIndex) {
16 return false;
17 } else if (unmoved && y == destinationY && Math.abs(x - destinationX) == 2
18 && !ChessPlayField.isCheckState(pieceGrid, participantOwnerIndex)) {
19 if (x > destinationX && pieceGrid[0 * GRID_HEIGHT + y] != null
20 && pieceGrid[0 * GRID_HEIGHT + y] instanceof ChessRook
21 && ((ChessRook) pieceGrid[0 * GRID_HEIGHT + y]).isUnmoved()
22 && pieceGrid[1 * GRID_HEIGHT + y] == null
23 && pieceGrid[(x - 1) * GRID_HEIGHT + y] == null) {
24 return true;
25 } else if (x < destinationX && pieceGrid[7 * GRID_HEIGHT + y] != null
26 && pieceGrid[7 * GRID_HEIGHT + y] instanceof ChessRook
27 && ((ChessRook) pieceGrid[7 * GRID_HEIGHT + y]).isUnmoved()
28 && pieceGrid[(x + 1) * GRID_HEIGHT + y] == null) {
29 return true;
30 } else {
31 return false;
32 }
33 } else if (Math.abs(x - destinationX) > 1 || Math.abs(y - destinationY) > 1) {
34 return false;
35 } else {
36 return true;
37 }
38 }
39
40 public void doMove(ChessPiece[] pieceGrid, int destinationX, int destinationY) {
41 if (Math.abs(x - destinationX) > 1) {
42 if (x > destinationX) {
43 ((ChessRook) pieceGrid[0 * GRID_HEIGHT + y]).doMove(pieceGrid, x - 1, y);
44 } else {
45 ((ChessRook) pieceGrid[7 * GRID_HEIGHT + y]).doMove(pieceGrid, x + 1, y);
46 }
47 }
48 super.doMove(pieceGrid, destinationX, destinationY);
49 unmoved = false;
50 }
51
52 public int getPieceType() {
53 return ChessPieceType.KING;
54 }
55
56 public int getPieceValue() {
57 return 100;
58 }
59
60 }